const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
/*const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin')
const ServiceWorkerWebpackPlugin = require('service-worker-precache-webpack-plugin');*/

module.exports = {
    mode:"production",
    plugins:[
        /*new ServiceWorkerWebpackPlugin({
            cacheId: 'Get3D-editor',
            dontCacheBustUrlsMatching: /\.\w{8}\./,
            filename: 'service-worker.js',
            minify: true,
            staticFileGlobs: [
                'src/libs/codemirror/codemirror.js',
                'src/libs/codemirror/mode/javascript.js',
                'src/libs/codemirror/mode/glsl.js',

                'src/libs/esprima.js',
                'src/libs/jsonlint.js',

                'src/libs/codemirror/addon/dialog.css',

                'src/Strings.js',
                'src/Toolbar.js',
                'src/ViewportMounter.js',
                'src/ViewportMounter.Camera.js',
                'src/ViewportMounter.Info.js',
                'src/ViewportMounter.ViewHelper.js',

            ],
            staticFileGlobsIgnorePatterns: [/\.map$/, /sw-manifest\.json$/],
        }),*/
        /*new ServiceWorkerWebpackPlugin({
            entry: path.join(__dirname, 'src/sw.js')
        }),*/
        // 限制chunks数量
        /*new webpack.optimize.LimitChunkCountPlugin({
            maxChunks: 5, // Minimum number of characters
        }),*/
        // 分割chunks
        /*new webpack.optimize.MinChunkSizePlugin({
            minChunkSize: 204800, // Minimum number of characters
        }),*/

        // “抽离css到单独的文件”
        new MiniCssExtractPlugin({
            filename: '/assets/css/[hash].css',
        }),
        // A Webpack plugin to optimize \ minimize CSS assets. 对css进行压缩和优化
        new OptimizeCSSAssetsPlugin({
            assetNameRegExp: /\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorOptions: {
                discardComments: { removeAll: true }
            },
            cssProcessorPluginOptions: {
                preset: ['default', {
                    discardComments: {
                        removeAll: true,
                    },
                    normalizeUnicode: false
                }]
            },
            canPrint: true
        })
    ],
    module: {
        rules: [
            /*{
                test: /\.js$/,
                use: { // 使用到的loader
                    loader: 'babel-loader',
                    options: {
                        presets: [],
                        plugins: [
                            '@babel/plugin-transform-runtime'
                        ]
                    }
                }
            },*/
            // 翻译 Babel-loader可以JSX / ES6文件转换成标准JS文件
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            // ['@babel/preset-env', { targets: "defaults" }]
                            ['@babel/preset-env', { targets: "defaults" }]
                        ],
                        plugins: ["@babel/plugin-transform-modules-commonjs",'@babel/plugin-proposal-class-properties','@babel/plugin-transform-runtime']
                    }
                }
            },
            {
                test: /\.css$/,

                use: [
                    // “抽离css到单独的文件”
                    MiniCssExtractPlugin.loader,
                    // ss-loader 解析 @import 和 url() ，然后 import/require() 加载
                    'css-loader',
                    // css 不同浏览器兼容
                    'postcss-loader',
                ]
            },
            // 图片转 Base64
            /*{
                test: /\.(png|svg|jpe?g)$/i,
                loader: 'url-loader',
                options: {
                    esModule: false
                }
            },*/

            {
                test: /\.(png|jpg|gif|svg|ico|hdr)$/,
                loader: 'file-loader',
                options: {
                    name: '[hash].[ext]',
                    outputPath: '/assets/images',
                    esModule: false,
                }
            },
            {
                test: /\.(eot|woff|woff2|ttf)(\?\S*)?$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]',
                    outputPath: '/assets/font',
                    esModule: false
                }
            },
            {
                type: 'javascript/auto',
                test: /\.(json)$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: '[hash].[ext]',
                        outputPath: '/assets/files',
                        esModule: false,
                    },
                }]
            }
        ]
    },
    optimization: {
        /*splitChunks: {
            chunks: 'initial',
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'common',
                    chunks: 'all'
                }
            }
        }*/
    }
}
